package dk.itu.pam.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dk.itu.pam.model.User;

/**
 * Servlet implementation class CheckUniqueEmail
 */
public class CheckUniqueEmail extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckUniqueEmail() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String result = "email is unique";
		String mail = request.getParameter("mail");
		System.out.println("CheckUniqueEmail received: " + mail);
		ServletContext c = getServletContext();
		ArrayList<User> users = (ArrayList<User>) c.getAttribute("users");
		for (int i=0; i< users.size(); i++) {
			if (users.get(i).getEmail().equalsIgnoreCase(mail)) {
				result = "email is not unique";
			}
		}
		PrintWriter out = response.getWriter();
		out.print(result);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
